class GameSystemTXZ extends Behaviour {

    object = {
        length: 1,
        height: 1
    };
    info = {
        position: null,
        data: [
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
            [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
            [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
            [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
            [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
            [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
            [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
            [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
            [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
            [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        ]
    };
    result = {};
    target = null;
    boxs = [];
    system = CollisionDetectionSystem;
    gameResult = false;

    onStart() {

    }

    setData() {
        if (this.target) {
            this.info.position = {
                x: this.target.getBehaviour(CanMoveButtonTxz).x,
                y: this.target.getBehaviour(CanMoveButtonTxz).y
            };
        }
        this.system.gameType = "tuixiangzi";
        this.system.setData(this.object, this.info.position, this.info.data);
    }

    initData(x: number, y: number, number: number) {
        this.info.data[y][x] = number;
    }

    onUpdate() {
        this.result = this.system.result;

        if (this.gameResult == false) {
            this.gameResult = this.checkResult();
            if (this.gameResult) {
                console.log("win");
            }
        }

        //console.log(this.info.data);
    }

    checkResult() {
        const point1 = core.getObjectById("point1").getBehaviour(CanMoveButtonTxz);
        const point2 = core.getObjectById("point2").getBehaviour(CanMoveButtonTxz);
        const point3 = core.getObjectById("point3").getBehaviour(CanMoveButtonTxz);
        const box1 = core.getObjectById("box1").getBehaviour(CanMoveButtonTxz);
        const box2 = core.getObjectById("box2").getBehaviour(CanMoveButtonTxz);
        const box3 = core.getObjectById("box3").getBehaviour(CanMoveButtonTxz);
        const a1 = [point1, point2, point3];
        const a2 = [box1, box2, box3];

        let mark = 0;

        for (let child1 of a1) {
            for (let child2 of a2) {
                if (child1.x == child2.x && child1.y == child2.y) {
                    mark++;
                }
            }
        }

        if (mark == 3) {
            return true;
        } else {
            return false;
        }
    }

    checkPosition() {
        const x = this.info.position.x;
        const y = this.info.position.y;

        let a1 = [
            {
                position: "left",
                x: x - 1,
                y: y
            },
            {
                position: "right",
                x: x + 1,
                y: y
            },
            {
                position: "up",
                x: x,
                y: y - 1
            },
            {
                position: "down",
                x: x,
                y: y + 1
            }
        ]
        let result = {
            up: null,
            down: null,
            left: null,
            right: null
        };

        for (let child of this.boxs) {
            for (let child2 of a1) {
                if (child.getBehaviour(CanMoveButtonTxz).x == child2.x &&
                    child.getBehaviour(CanMoveButtonTxz).y == child2.y) {
                    switch (child2.position) {
                        case "up":
                            result.up = child;
                            break;
                        case "down":
                            result.down = child;
                            break;
                        case "left":
                            result.left = child;
                            break;
                        case "right":
                            result.right = child;
                            break;
                    }
                }
            }

        }

        return result;
    }
}

class GameInitSystemTXZ extends Behaviour {

    image = null;
    data = null;

    onStart() {
        //console.log(this)
        this.getData();
        this.initGameObject();
        this.gameObject.getBehaviour(GameSystemTXZ).target = core.getObjectById("player");
        this.gameObject.getBehaviour(GameSystemTXZ).setData();
    }

    getData() {
        this.image = gameProcess.image;
        this.data = gameProcess.gameData;
    }

    initGameObject() {
        const system = this.gameObject.getBehaviour(GameSystemTXZ);
        const parent = core.getObjectById("objects");
        const walls = core.getObjectById("walls");
        const keys = Object.keys(this.data);

        for (let key of keys) {

            if (key == "wall") {
                for (let child of this.data[key].data) {
                    const obj = new GameObject();
                    const transform = new Transform();
                    const imageRenderer = new ImageRenderer();
                    obj.id = key;
                    transform.x = child.x * 59;
                    transform.y = child.y * 59;
                    imageRenderer.image = this.image.wall;
                    obj.addBehaviour(transform);
                    obj.addBehaviour(imageRenderer);
                    walls.addChild(obj);
                    core.addToIds(obj.id, obj);
                    system.initData(child.x, child.y, 1);
                }

            } else {
                const obj = new GameObject();
                const transform = new Transform();
                const button = new CanMoveButtonTxz();
                obj.id = key;
                transform.x = this.data[key].x * 59;
                transform.y = this.data[key].y * 59;

                switch (this.data[key].type) {
                    case "player":
                        button.image = this.image.player;
                        system.initData(this.data[key].x, this.data[key].y, 1);
                        break;
                    case "box1":
                        button.image = this.image.box1;
                        system.initData(this.data[key].x, this.data[key].y, 1);
                        break;
                    case "box2":
                        button.image = this.image.box2;
                        system.initData(this.data[key].x, this.data[key].y, 1);
                        break;
                    case "box3":
                        button.image = this.image.box3;
                        system.initData(this.data[key].x, this.data[key].y, 1);
                        break;
                    case "point":
                        button.isMovable = false;
                        button.image = this.image.point;
                        //console.log(this.image)
                        break;
                }
                button._height = 1;
                button._length = 1;
                button.x = this.data[key].x;
                button.y = this.data[key].y;
                obj.addBehaviour(transform);
                obj.addBehaviour(button);
                parent.addChild(obj);
                core.addToIds(obj.id, obj);
                if (key == "box1" || key == "box2" || key == "box3") {
                    system.boxs.push(core.getObjectById(key));
                }
            }


        }
    }
}

class MoveButtonTxz extends Button {

    @SerializedField('')
    direction = "";

    onStart() {

        this.onClick = () => {

            const system = core.getObjectById("gameSystemTXZ").getBehaviour(GameSystemTXZ);
            const result = core.getObjectById("gameSystemTXZ").getBehaviour(GameSystemTXZ).result;
            const target = core.getObjectById("gameSystemTXZ").getBehaviour(GameSystemTXZ).target;

            const aroundResult = system.checkPosition();
            //console.log(aroundResult)
            switch (this.direction) {
                case 'up':
                    if (aroundResult.up) {
                        if (!result.up.otherObject) {
                            break;
                        } else {
                            this.changPosition(target, "y", 1);
                            this.changPosition(aroundResult.up, "y", 1);
                        }
                    } else if (result.up.thisObject) {
                        this.changPosition(target, "y", 1);
                    }
                    system.setData();
                    break;
                case 'down':
                    if (aroundResult.down) {
                        if (!result.down.otherObject) {
                            break;
                        } else {
                            this.changPosition(target, "y", -1);
                            this.changPosition(aroundResult.down, "y", -1);
                        }
                    } else if (result.down.thisObject) {
                        this.changPosition(target, "y", -1);
                    }
                    system.setData();
                    break;
                case 'left':
                    if (aroundResult.left) {
                        if (!result.left.otherObject) {
                            break;
                        } else {
                            this.changPosition(target, "x", 1);
                            this.changPosition(aroundResult.left, "x", 1);
                        }
                    } else if (result.left.thisObject) {
                        this.changPosition(target, "x", 1);
                    }
                    system.setData();
                    break;
                case 'right':
                    if (aroundResult.right) {
                        if (!result.right.otherObject) {
                            break;
                        } else {
                            this.changPosition(target, "x", -1);
                            this.changPosition(aroundResult.right, "x", -1);
                        }
                    } else if (result.right.thisObject) {
                        this.changPosition(target, "x", -1);
                    }
                    system.setData();
                    break;
            }
        }
    }

    changPosition(object: GameObject, position: string, number: number) {
        object.getBehaviour(CanMoveButtonTxz).initData(0);
        switch (position) {
            case "x":
                if (number == 1) {
                    object.getBehaviour(Transform).x -= 59;
                } else {
                    object.getBehaviour(Transform).x += 59;
                }
                object.getBehaviour(CanMoveButtonTxz).x -= number;
                break;
            case "y":
                if (number == 1) {
                    object.getBehaviour(Transform).y -= 59;
                } else {
                    object.getBehaviour(Transform).y += 59;
                }
                object.getBehaviour(CanMoveButtonTxz).y -= number;
                break;
        }
        object.getBehaviour(CanMoveButtonTxz).initData(1);
    }
}

class CanMoveButtonTxz extends Button {

    id = "";
    // @SerializedField(0)
    x = 0;
    // @SerializedField(0)
    y = 0;
    // @SerializedField(0)
    _length = 0;
    // @SerializedField(0)
    _height = 0;
    isMovable = true;

    onAddToGameObject() {

        super.onAddToGameObject();

        if (this.isMovable) {
            this.onClick = () => {
                console.log(this.gameObject.id);
            }
        }
        this.id = this.gameObject.id;


        //this.initData(1);

    }

    initData(number) {
        core.getObjectById("gameSystemTXZ").getBehaviour(GameSystemTXZ).initData(this.x, this.y, number);
    }

}

class ReStartButtonTxz extends Button {

    onStart() {
        this.onClick = () => {
            core.changeScene("./data/scenes/tuixiangzi.json");
        }
    }
}

class FinishGameButtonTxz extends Button {

    onStart() {
        this.onClick = () => {
            gameProcess.setGameResult(core.getObjectById("gameSystemTXZ").getBehaviour(GameSystemTXZ).gameResult);
            gameProcess.changeSceneBack("morework");
        }
    }
}

core.registerBehaviourClass(GameSystemTXZ);
core.registerBehaviourClass(GameInitSystemTXZ);
core.registerBehaviourClass(MoveButtonTxz);
core.registerBehaviourClass(CanMoveButtonTxz);
core.registerBehaviourClass(ReStartButtonTxz);
core.registerBehaviourClass(FinishGameButtonTxz);